<?php
include("include/config.inc.php");

$pageName = 'bill';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}

if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:index.php");
}
else
{

	$partyId = isset($_POST['partyId']) ? $_POST['partyId'] : 0;
	$partyName = "";
	$incomingRegister = 0;
	$selMaxBill = "SELECT MAX(billNo) + 1 AS billNo FROM billmaster";
	$selMaxBillRes = mysql_query($selMaxBill);
	if($noRow = mysql_fetch_array($selMaxBillRes))
	{
    if($noRow['billNo'] != NULL)
    {
      $billNo = $noRow['billNo'];
    }
    else
    {
    	$billNo = 1;
    }
	}
	if(isset($_POST['generateBill']) && isset($_POST['checkedOutMasterId']))
	{
		$billDate         = $_POST['billDateYear']."-".$_POST['billDateMonth']."-".$_POST['billDateDay'];
		$notes            = isset($_POST['notes']) && $_POST['notes'] != '' ? $_POST['notes'] : '' ;
		$total            = isset($_POST['total']) && $_POST['total'] != '' ? $_POST['total'] : 0 ;
		$serviceTax       = isset($_POST['serviceTax']) && $_POST['serviceTax'] != '' ? $_POST['serviceTax'] : 0 ;
		$roundOff       = isset($_POST['roundOff']) && $_POST['roundOff'] != '' ? $_POST['roundOff'] : 0 ;
		$netTotal         = isset($_POST['netTotal']) && $_POST['netTotal'] != '' ? $_POST['netTotal'] : 0 ;
		$taxInPrecentage  = isset($_POST['taxInPrecentage']) && $_POST['taxInPrecentage'] != '' ? $_POST['taxInPrecentage'] : 0 ;
		$insertBillMaster = "INSERT INTO billmaster (billNo, date, partyId, total, taxInPrecentage, serviceTax, roundOff, netTotal, notes)
		                     VALUES (".$_POST['billId'].",'".$billDate."', ".$_POST['billPartyId'].", ".$total.", ".$taxInPrecentage.",
		                             ".$serviceTax.", ".$roundOff.", ".$netTotal.", '".$notes."')";
    $insertBillMasterRes = mysql_query($insertBillMaster);
    $insertedMasterId = mysql_insert_id();
    if($insertBillMasterRes)
    {
      foreach($_POST['checkedOutMasterId'] as $key => $value)
      {
      	if($_POST['calcWght'][$key] != '')
      	{
      		$calcWith = 'W';
      	}
      	else
      	{
      		$calcWith = 'Q';
      	}
        $insertBillDeatail = "INSERT INTO billdetail (billMasterId, outGoingMasterId, finalProcess, outQuantity, outWieght, outRate, calcWith, amount)
                              VALUES (".$insertedMasterId.",".$value.", ".$_POST['finalProcess'][$key].", ".$_POST['outQuantity'][$key].",
                                      ".$_POST['outWeight'][$key].", ".$_POST['outRate'][$key].", '".$calcWith."', ".$_POST['outAmount'][$key].")";
        $insertBillDeatailRes = mysql_query($insertBillDeatail);
        if($insertBillDeatailRes)
        {
        	$update = "UPDATE outgoingmaster SET billGenerated = '1' WHERE outgoingMasterId = ".$value;
        	$updateRes = mysql_query($update);
        }
      }
    }
	}
	
	if(isset($_POST['partyId']) && $_POST['partyId'] > 0)
	{
	  $incomingRegister = array();
	  $i =0;
	  $selectincomingRegister = "SELECT outgoingMasterId,outgoingmaster.incomingId,outChallenNo,partyName,
	                                    DATE_FORMAT(incoming.IncomingDate,'%d-%m-%Y')AS IncomingDate,incomingQuantity,challan,incomingWeight,
	                                    processName1.processName AS processName1,processName2.processName AS processName2,
	                                    processName3.processName AS processName3,materialName,hardne,dispatchQuantity,dispatchWeight
	                               FROM outgoingmaster
	                               JOIN incoming ON outgoingmaster.incomingId = incoming.incomingId
	                               LEFT JOIN party ON party.partyId = incoming.partyId
	                               LEFT JOIN process AS processName1 ON processName1.processId = incoming.processId1
	                               LEFT JOIN process AS processName2 ON processName2.processId = incoming.processId2
	                               LEFT JOIN process AS processName3 ON processName3.processId = incoming.processId3
	                               LEFT JOIN material ON material.materialId = incoming.materialId
	                              WHERE (outQuantity > '0' OR outWeight > '0')
	                                AND billGenerated = '0'
	                                AND incoming.partyId = ".$_POST['partyId'];
	  $selectincomingRegisterRes = mysql_query($selectincomingRegister);
	  while($incomingRow = mysql_fetch_array($selectincomingRegisterRes))
	  {
	  	$incomingRegister[$i]['outgoingMasterId'] = $incomingRow['outgoingMasterId'];
	  	$incomingRegister[$i]['incomingId']   = $incomingRow['incomingId'];
	  	$incomingRegister[$i]['outChallenNo'] = $incomingRow['outChallenNo'];
	  	$partyName                            = $incomingRow['partyName'];
	  	$incomingRegister[$i]['IncomingDate'] = $incomingRow['IncomingDate'];
	  	$incomingRegister[$i]['qty']          = $incomingRow['incomingQuantity'];
	  	$incomingRegister[$i]['challan']      = $incomingRow['challan'];
	  	$incomingRegister[$i]['weight']       = $incomingRow['incomingWeight'];
	  	$incomingRegister[$i]['processName1'] = $incomingRow['processName1'];
	  	$incomingRegister[$i]['processName2'] = $incomingRow['processName2'];
	  	$incomingRegister[$i]['processName3'] = $incomingRow['processName3'];
	  	$incomingRegister[$i]['material']     = $incomingRow['materialName'];
	  	$incomingRegister[$i]['hardne']       = $incomingRow['hardne'];
	  	$incomingRegister[$i]['outQuantity']  = $incomingRow['dispatchQuantity'];
	  	$incomingRegister[$i]['outWeight']    = $incomingRow['dispatchWeight'];
	  	$incomingRegister[$i]['rowCount']     = $i;
	  	$incomingRegister[$i]['billNo']       = $billNo;
	  	$i++;
	  }
	}
  $partyArray = array();
  $k = 0;
  $select = "SELECT partyId,partyName
               FROM party
              WHERE partyId IN (SELECT partyId FROM incoming WHERE outgoingDone = 1)
              ORDER BY partyName";
  $selectResult = mysql_query($select);
  while($row = mysql_fetch_array($selectResult))
  {
    $partyArray['partyId'][$k]   = $row['partyId'];
    $partyArray['partyName'][$k] = $row['partyName'];
    $k++;
  }
  
  $fpArray = array();
  $fp = 0;
  $selectFP = "SELECT finalProcessId,finalProcessName
                 FROM finalprocess
                ORDER BY finalProcessName";
  $selectFPResult = mysql_query($selectFP);
  while($fpRow = mysql_fetch_array($selectFPResult))
  {
    $fpArray['finalProcessId'][$fp]   = $fpRow['finalProcessId'];
    $fpArray['finalProcessName'][$fp] = $fpRow['finalProcessName'];
    $fp++;
  }
  
  include("./bottom.php");
  $smarty->assign("msg",'<font size="6">No Record Found</font>');
	$smarty->assign("billNo",$billNo);
	$smarty->assign("partyName",$partyName);
	$smarty->assign("partyId",$partyId);
	$smarty->assign("todaysDate",date('d-m-Y'));
	$smarty->assign("partyArray",$partyArray);
	$smarty->assign("fpArray",$fpArray);
	$smarty->assign("incomingRegister",$incomingRegister);
  $smarty->display('bill.tpl');
}
?>